/*
 A class to help view a scene in Google Earth.
*/

const REFRESHHREF = (Site.DEBUG)? 'http://localhost:8080/live3dstatic/refresh.dae' : 'http://www.projectlive3d.com/live3dstatic/refresh.dae' 
const MODELHREF   = (Site.DEBUG)? 'http://localhost:8080/renderMesh/'              : 'http://www.projectlive3d.com/renderMesh/'
const IMAGEHREF   = (Site.DEBUG)? 'http://localhost:8080/renderOriginalImage/'     : 'http://www.projectlive3d.com/renderOriginalImage/'

function ViewScene(url, sceneId){
	this.url = url;
	this.sceneId = sceneId;
	if (document.getElementById('smallimage')){
		this.img = document.getElementById('smallimage');
	} else{
		this.img = null;
	}
	
	this.updateImage();
	
	this.models = new Array();
	this.modelVisible = new Array();
	this.embedVisible = false;
}

ViewScene.prototype.initAMOS = function(amosId){
	this.amos = new AMOS(amosId, this);
}

ViewScene.prototype.amosClick = function(ev){
	this.amos.click(ev);
}

ViewScene.prototype.updateAMOSImage = function(){
	if (this.amos && this.amos.imageURL){
		this.imageURL = this.amos.imageURL
		this.img.setAttribute("src", this.amos.imageURL);
	}
}

ViewScene.prototype.addModel = function(id, lat, lon, alt){
	var modelHref = MODELHREF + id + '.dae'
	var kmlString =   
		'<Placemark>'+
		'  <Model id="myModel">'+
		'    <altitudeMode>absolute</altitudeMode>'+
		'    <Location>'+
		'      <longitude>' + lon + '</longitude>'+
		'      <latitude>' + lat + '</latitude>'+
		'      <altitude>' + (alt + 0.1) + '</altitude>'+
		'    </Location>'+
		'    <Link>'+
		'      <href>' + modelHref + '</href>'+
		'    </Link>'+
		'    <ResourceMap>'+
		'      <Alias>'+
		'        <targetHref>' + encodeHTML(this.imageURL) +'</targetHref>'+
		'        <sourceHref>image.jpg</sourceHref>'+
		'      </Alias>'+
		'    </ResourceMap>'+
		'  </Model>'+
		'</Placemark>';
	
	var placemark = ge.parseKml(kmlString);
	this.models[id] = {};
	this.models[id].placemark = placemark;
	this.models[id].visible = false;
	this.models[id].lat = lat;
	this.models[id].lon = lon;
	this.models[id].alt = alt;
	this.turnOnModel(id);
}

ViewScene.prototype.makeRefresher = function(){
	// find the mean location.
	var sumlat = 0,   sumlon = 0,   sumalt = 0;
	var countlat = 0, countlon = 0, countalt = 0;
	for (var id in this.models){
		sumlat = sumlat + this.models[id].lat;
		sumlon += this.models[id].lon;
		sumalt += this.models[id].alt;
		countlat++;
		countlon++;
		countalt++;
	}
	var lat = sumlat / countlat;
	var lon = sumlon / countlon;
	var alt = sumalt / countalt;
	
	// make the refresher.
	var kmlString =   
		'<Placemark>'+
		'  <Model id="myModel">'+
		'    <altitudeMode>absolute</altitudeMode>'+
		'    <Location>'+
		'      <longitude>' + lon + '</longitude>'+
		'      <latitude>' + lat + '</latitude>'+
		'      <altitude>' + (alt + 0.1) + '</altitude>'+
		'    </Location>'+
		'    <Link>'+
		'      <href>' + REFRESHHREF + '</href>'+
		'    </Link>'+
		'    <ResourceMap>'+
		'      <Alias>'+
		'        <targetHref>' + encodeHTML(this.imageURL)  +'</targetHref>'+
		'        <sourceHref>image.jpg</sourceHref>'+
		'      </Alias>'+
		'    </ResourceMap>'+
		'  </Model>'+
		'</Placemark>';
	
	var placemark = ge.parseKml(kmlString);
	
	// make the placemark refresh a lot.
	var link = placemark.getGeometry().getLink();
	link.setRefreshMode(ge.REFRESH_ON_INTERVAL);
	link.setRefreshInterval(1.0);
	
	// make the geometry invisible.
	placemark.getGeometry().getScale().setX(0.0);
	placemark.getGeometry().getScale().setY(0.0);
	placemark.getGeometry().getScale().setZ(0.0);
	
	ge.getFeatures().appendChild(placemark);
	this.refresher = placemark;
}

ViewScene.prototype.changeTexture = function(){
	this.updateImage();
	for (var id in this.models){
		ge.getFeatures().removeChild(this.models[id].placemark);
		this.addModel(id,  this.models[id].lat,  this.models[id].lon,  this.models[id].alt);
		
	}
}

ViewScene.prototype.turnOnModel = function(id){
	ge.getFeatures().appendChild(this.models[id].placemark);
	this.models[id].visible = true;
	if (document.getElementById('mesh' + id))
		document.getElementById('mesh' + id).checked = true;
}

ViewScene.prototype.turnOffModel = function(id){
	ge.getFeatures().removeChild(this.models[id].placemark);
	this.models[id].visible = false;
	if (document.getElementById('mesh' + id))
		document.getElementById('mesh' + id).checked = false;
}

ViewScene.prototype.toggleModel = function(id){
	if (document.getElementById('mesh' + id)){
		var modelOn = document.getElementById('mesh' + id).checked;
		if (modelOn){
			this.turnOnModel(id);
		} else {
			this.turnOffModel(id);
		}
	}
}

ViewScene.prototype.selectAll = function(){
	for (var id in this.models){
		this.turnOnModel(id)
	}
}

ViewScene.prototype.unselectAll = function(){
	for (var id in this.models){
		this.turnOffModel(id)
	}
}

ViewScene.prototype.getTextureMode = function(){
	var selection = -1;
	if (document.getElementById('textureForm')){
		var options = document.getElementById("textureForm").elements;
		for (i = 0; i < options.length; i++){
			if (options[i].checked){
				selection = options[i].value;
				break;
			}
		}
	} else {
		selection = 'current';
	}
	this.textureMode = selection;
}

ViewScene.prototype.refreshImage = function(){
	this.getTextureMode();
	if (this.textureMode == 'current'){
		var tmp = new Date();
		tmp = "?"+tmp.getTime() 
		this.img.setAttribute('src', viewScene.url + tmp)
	}
}

ViewScene.prototype.updateImage = function(){
	this.getTextureMode();
	
	if (this.img == null){
		this.imageURL = this.url;
		return;
	}
	
	if (this.textureMode == 'current'){
		this.imageURL = this.url
		this.img.setAttribute("src", this.imageURL);
		document.getElementById('datetime').innerHTML = 'Most recent image';
		if (document.getElementById('timelapse_control'))
			document.getElementById('timelapse_control').style.setProperty('display', 'none', null);
	}
	else if (this.textureMode == 'original'){
		this.imageURL = IMAGEHREF + this.sceneId + ".jpg"
		this.img.setAttribute("src", this.imageURL);
		document.getElementById('datetime').innerHTML = 'First image recorded';
		if (document.getElementById('timelapse_control'))
			document.getElementById('timelapse_control').style.setProperty('display', 'none', null);
	}
	else if (this.textureMode == 'timelapse'){
		if (this.amos && this.amos.imageURL){
			this.imageURL = this.amos.imageURL;
			this.img.setAttribute("src", this.imageURL);
		} else{
			this.imageURL = this.url
			this.img.setAttribute("src", this.imageURL);
			document.getElementById('datetime').innerHTML = 'Most recent image';
		}
		document.getElementById('timelapse_control').style.setProperty('display', 'block', null);
	}
}